package com.haibo.exception;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

/**
 * @Author : 刘海波
 * @Date: 2024/12/25 10:36
 * @Description:
 */
@Data
public class Response<T> {
    /**
     * 0：成功
     * -1：失败
     */
    @JsonProperty("hasError")
    private String hasError = null;
    @JsonProperty("errorMessage")
    private String errorMessage = null;
    private T data;
    private Integer code;
    private String message;


    public static <T> Response<T> success() {
        Response<T> response = new Response<>();
        response.setHasError("0");
        response.setErrorMessage("");
        return response;
    }

    public static <T> Response<T> success(Integer code,String message) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMessage("成功");
        return response;
    }

    public static <T> Response<T> success(T data) {
        Response<T> response = new Response<>();
        response.setHasError("0");
        response.setErrorMessage("");
        response.setData(data);
        return response;
    }

    public static <T> Response<T> fail(String message) {
        Response<T> response = new Response<>();
        response.setHasError("1");
        response.setErrorMessage(message);
        return response;
    }

    public static <T> Response<T> fail(Integer code,String message) {
        Response<T> response = new Response<>();
        response.setCode(code);
        response.setMessage(message);

        return response;
    }

    public Response<T> data(T entity) {
        this.setData(entity);
        return this;
    }

}
